﻿ 
using Newtonsoft.Json;
using System;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;

namespace ParkingDataSimulate
{ 
    public partial class ResponseModalX
    {
        public ResponseModalX()
        {
            _meta = new MetaModalX { Success = true, Message = "OK", ErrorCode = -1 };
            _data = null;
        }
        private MetaModalX _meta;
        [JsonProperty("meta")]
        public MetaModalX meta
        {
            get
            {
                return _meta;
            }
            set
            {
                _meta = value;
            }
        }
        private Object _data;
        [JsonProperty("data")]
        public Object data
        {
            get
            {
                return _data;
            }
            set
            {
                _data = value;
            }
        }

        public static implicit operator Task<object>(ResponseModalX v)
        {
            throw new NotImplementedException();
        }
         
        public static R Mapping<R, T>(T model)
        {
            R result = Activator.CreateInstance<R>();
            foreach (PropertyInfo info in typeof(R).GetProperties())
            {
                PropertyInfo pro = typeof(T).GetProperty(info.Name);
                if (pro != null)
                    info.SetValue(result, pro.GetValue(model));
            }
            return result;
        }

        public static ApiUrlConfig Get()
        {
            string apiUrlConfigJosn;
            string root = AppDomain.CurrentDomain.BaseDirectory;
            string pathFileName = Path.Combine(root, "ApiUrlConfig.json");
            apiUrlConfigJosn = CommonBase.ReadConfigJson(pathFileName);
            ApiUrlConfig apiUrlConfig = JsonConvert.DeserializeObject<ApiUrlConfig>(apiUrlConfigJosn);
            return apiUrlConfig;
        }
    }
    public partial class MetaModalX
    {
        [JsonProperty("success")]
        public bool Success { get; set; } = true;

        [JsonProperty("message")]
        public string Message { get; set; } = "OK";

        [JsonProperty("errorCode")]
        public int ErrorCode { get; set; } = -1;
    }

    public class PictureUploadRet
    { 
        public string MainComId { get; set; }
        public string SubPath { get; set; }
        public string NewFileName { get; set; }
        public string RawFileName { get; set; }
        public string TargetTalbeKeyId { get; set; }
        public string Extension { get; set; }
        public string PicClientUrl { get; set; } 
        public string AsolutionPicClientPath { get; set; }
    }
    public class ApiUrlConfig
    { 
        /// <summary>
        /// http Or https
        /// 
        /// </summary>
        private string _Schema;
        /// <summary>
        /// http Or https
        /// </summary>
        public string Schema
        {
            get
            {
                return _Schema;
            }
            set
            {
                _Schema = value;
            }
        }
        private string _Host;
        /// <summary>
        /// 192.168.1.14  Or www.abcdrf.com
        /// </summary>
        public string Host
        {
            get
            {
                return _Host;
            }
            set
            {
                _Host = value;
            }
        }
        private string _Port;
        /// <summary>
        /// 80, 8080 
        /// </summary>
        public string Port
        {
            get
            {
                return _Port;
            }
            set
            {
                _Port = value;
            }
        }
        private string _UserName;
        public string UserName
        {
            get
            {
                return _UserName;
            }
            set
            {
                _UserName = value;
            }
        }
        private string _Password;
        public string Password
        {
            get
            {
                return _Password;
            }
            set
            {
                _Password = value;
            }
        }

        public static ApiUrlConfig Get()
        {
            string apiUrlConfigJosn;
            string root = AppDomain.CurrentDomain.BaseDirectory;
            string pathFileName = Path.Combine(root, "ApiUrlConfig.json");
            int digits = new Random(Guid.NewGuid().GetHashCode()).Next(200000, 900000);
            string mailname = RamdomLetter();
            string acc = $"{mailname}{digits}@mail.com";
            if (File.Exists(pathFileName) == false)
            {
                ApiUrlConfig apiUrlConfigX = new ApiUrlConfig
                {
                    Schema = "https",
                    Host = "localhost",
                    Port = "5001",
                    UserName = acc,
                    Password = digits.ToString()
                };
                return apiUrlConfigX;
            }

            apiUrlConfigJosn = CommonBase.ReadConfigJson(pathFileName);
 
            ApiUrlConfig apiUrlConfig = JsonConvert.DeserializeObject<ApiUrlConfig>(apiUrlConfigJosn);
            return apiUrlConfig;
        }
        public static string  RamdomLetter()
        {
            var characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
            var Charsarr = new char[8];
            var random = new Random();

            for (int i = 0; i < Charsarr.Length; i++)
            {
                Charsarr[i] = characters[random.Next(characters.Length)];
            }

            var resultString = new String(Charsarr);
            return resultString;
        }
    }

    public class LoginDto
    {
        private string _UserName;
        public string UserName
        {
            get
            {
                return _UserName;
            }
            set
            {
                _UserName = value;
            }
        }
        private string _Password;
        public string Password
        {
            get
            {
                return _Password;
            }
            set
            {
                _Password = value;
            }
        }
    }

    public class VAXTORJson
    {
        public string Plate { get; set; }
        public string Date { get; set; }
        public DateTime OccuredDatetime { get; set; }
        public string Confidence { get; set; }
        public string Country { get; set; }
        public string Ip { get; set; }
        public string Image64 { get; set; }
        public int ParkingNoCalcType { get; set; }
        public static VAXTORJson Get(int number)
        {
            string root = AppContext.BaseDirectory; //AppDomain.CurrentDomain.BaseDirectory;
            string pathFileName = Path.Combine(root, $"VAXTOR{number}.json");

            //int digits = new Random(Guid.NewGuid().GetHashCode()).Next(200000, 900000);
            
            if (File.Exists(pathFileName) == false)
            {
                VAXTORJson vAXTORJson = new VAXTORJson
                { 
                    Date = DateTime.UtcNow.ToString(),
                    Ip = "192.168.0.111",
                    Confidence = "99",
                    Country = "HongKong",
                    Image64 = VAXTORBASE.Image64,
                    Plate = "AB1C1",
                    ParkingNoCalcType = 0
                };
                return vAXTORJson;
            }

            string vAXTORJsonstr = CommonBase.ReadConfigJson(pathFileName);

            VAXTORJson vAXTOR = JsonConvert.DeserializeObject<VAXTORJson>(vAXTORJsonstr);
            vAXTOR.Date = DateTime.UtcNow.ToString();
            //vAXTOR.ip = "192.168.0.111";  
            //vAXTOR.plate = "AB1C1";

            return vAXTOR;
        }
    }
}
